﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace Bids.Core.Common.Data
{
    /// <summary>
    /// 字符串操作类
    /// </summary>
    public static class StringHandler
    {
        /// <summary>
        /// 计算匹配个数
        /// </summary>
        /// <param name="content">全部字符串</param>
        /// <param name="pattern">要匹配的内容</param>
        /// <returns></returns>
        public static int MachCount(string content, string pattern)
        {
            return new Regex(pattern, RegexOptions.IgnoreCase).Matches(content).Count;
        }

        

        /// <summary>
        /// 截取字符串，如果字符长度超过则截取
        /// </summary>
        /// <param name="content">全部字符</param>
        /// <param name="Length">要截取的最大长度</param>
        /// <returns></returns>
        public static string Substring(string content, int length)
        {
            if (content != null && content != string.Empty)
            {
                if (content.Length > length)
                {
                    content = content.Substring(0, length);
                }
            }
            return content;
        }

        /// <summary>
        /// 过滤危险数据库字符
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string SafeCode(string content)
        {
            if (content != null && content != string.Empty)
            {
                content = content.Replace("'", string.Empty).Replace(",", string.Empty).Replace(";", string.Empty);
            }
            return content;
        }

        /// <summary>
        /// 不支持HTML代码的过滤方法
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string DisableHTML(string content)
        {
            if (content != null && content != string.Empty)
            {
                content = content.Replace("<", "&lt;");
                content = content.Replace(">", "&gt;");
            }
            return content;
        }
        /// <summary>
        /// 清除HTML代码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string ClearHTML(this string content)
        {
            if (content != null && content != string.Empty)
            {
                content = System.Text.RegularExpressions.Regex.Replace(content, "<[^>]+>", "");
            }
            return content;
        }
        /// <summary>
        /// 过滤危险JS代码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string DisableJS(string content)
        {
            if (content != null && content != string.Empty)
            {
                content = content.Replace("/*", "\\*");
                content = content.Replace("*/", "*\\");
                string script = "position|left|top";
                foreach (string s in script.Split('|'))
                {
                    content = Regexs(content, @"( style[\s]*=[\s\S]*?)" + s + @"[\s]*:[\s]*\w+[\s;]*", @"$1");
                }
                string code = "<script|</script|cookie|alert|expression|eval|escape|write";
                foreach (string s in code.Split('|'))
                {
                    content = Regexs(content, s, "<span>" + s.Substring(0, 1) + "</span>" + s.Substring(1));
                }
            }
            return content;
        }
        /// <summary>
        /// 过滤Iframe代码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string DisableIframe(string content)
        {
            if (content != null && content != string.Empty)
            {
                string code = "<iframe|</iframe|<frame|</frame";
                foreach (string s in code.Split('|'))
                {
                    content = Regexs(content, s, "&lt;" + s.Substring(1));
                }
            }
            return content;
        }

        /// <summary>
        /// 检查内容是否为空
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool IsEmpty(string content)
        {
            bool result = false;
            if (content != null)
            {
                if (content.ToLower().Replace("&nbsp;", string.Empty).Replace("<br>", string.Empty).Replace("<p>", string.Empty).Replace("</p>", string.Empty).Trim() == string.Empty)
                {
                    result = true;
                }
            }
            else
            {
                result = true;
            }
            return result;
        }

        public static string Regexs(string html, string str1, string str2)
        {
            Regex re = new Regex(str1, RegexOptions.IgnoreCase);
            return re.Replace(html, str2);
        }

        

        /// <summary>
        /// 检查是否有UBB标签
        /// </summary>
        /// <param name="content">要检查的字符串</param>
        /// <returns></returns>
        public static bool HasUBB(string content)
        {
            bool result = false;
            if (content != null && content != string.Empty)
            {
                Regex r;
                //查找[b][/b]标记
                r = new Regex(@"(\[b\])((.|\n)*?)(\[\/b\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[i][/i]标记
                r = new Regex(@"(\[i\])((.|\n)*?)(\[\/i\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[u][/u]标记
                r = new Regex(@"(\[u\])([ \S\t]*?)(\[\/u\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[sup][/sup]标记
                r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[sub][/sub]标记
                r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[url][/url]标记
                r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[url=xxx][/url]标记
                r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[email][/email]标记
                r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[email=xxx][/email]标记
                r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[size=x][/size]标记
                r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[color=x][/color]标记
                r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[font=x][/font]标记
                r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找图片链接
                r = new Regex(@"(\[picture\])([ \S\t]*?)(\[\/picture\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[align=x][/align]
                r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[H=x][/H]标记
                r = new Regex(@"(\[h=([1-6])\])([ \S\t]*?)(\[\/h\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[list=x][*][/list]
                r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[SHADOW=x][/SHADOW]标记
                r = new Regex(@"(\[shadow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/shadow\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[glow=x][/glow]标记
                r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[center][/center]标记
                r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[em]
                r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[img][/img]标记
                r = new Regex(@"(\[img\])([ \S\t]*?)(\[\/img\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[file][/file]标记
                r = new Regex(@"(\[file\])([ \S\t]*?)(\[\/file\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[flash=x,y][/flash]标记
                r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[flash][/flash]标记
                r = new Regex(@"(\[flash\])([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[swf][/swf]标记
                r = new Regex(@"(\[swf\])([ \S\t]*?)(\[\/swf\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[dir=x][/dir]标记
                r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[rm=x,y][/rm]标记
                r = new Regex(@"(\[rm=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[mp=x,y][/mp]标记
                r = new Regex(@"(\[mp=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[qt=x][/qt]标记
                r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[QUOTE][/QUOTE]标记
                r = new Regex(@"(\[quote\])((.|\n)*?)(\[\/quote\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[CODE][/CODE]标记
                r = new Regex(@"(\[code\])((.|\n)*?)(\[\/code\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[move][/move]标记
                r = new Regex(@"(\[move\])((.|\n)*?)(\[\/move\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[FLY][/FLY]标记
                r = new Regex(@"(\[fly\])((.|\n)*?)(\[\/fly\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[image][/image]标记
                r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
                //查找[download=x][/download]标记
                r = new Regex(@"(\[download=([ \S\t]*?)\])([ \S\t]*?)(\[\/download\])", RegexOptions.IgnoreCase);
                if (r.IsMatch(content))
                {
                    return true;
                }
            }
            return result;
        }

        /// <summary>
        /// 过滤UBB代码
        /// </summary>
        /// <param name="content">要过滤的字符串</param>
        /// <returns></returns>
        public static string ClearUBB(string content)
        {
            string result = string.Empty;
            if (content != null && content != string.Empty)
            {
                Regex r;
                Match m;
                result = content;
                //处[b][/b]标记
                r = new Regex(@"(\[b\])((.|\n)*?)(\[\/b\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[i][/i]标记
                r = new Regex(@"(\[i\])((.|\n)*?)(\[\/i\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[u][/u]标记
                r = new Regex(@"(\[u\])([ \S\t]*?)(\[\/u\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[sup][/sup]标记
                r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[sub][/sub]标记
                r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[email][/email]标记
                r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[email=xxx][/email]标记
                r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处[size=x][/size]标记
                r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处[color=x][/color]标记
                r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处[font=x][/font]标记
                r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处理图片链接
                r = new Regex(@"(\[picture\])([ \S\t]*?)(\[\/picture\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处理[align=x][/align]
                r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处[H=x][/H]标记
                r = new Regex(@"(\[h=([1-6])\])([ \S\t]*?)(\[\/h\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处理[list=x][*][/list]
                r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    string strLI = m.Groups[5].ToString();
                    Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
                    Match mLI;
                    for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
                    {
                        strLI = strLI.Replace(mLI.Groups[0].ToString(), "<LI>" + mLI.Groups[1]);
                    }
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[4].ToString());
                }
                //处[SHADOW=x][/SHADOW]标记
                r = new Regex(@"(\[shadow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/shadow\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[5].ToString());
                }
                //处[glow=x][/glow]标记
                r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[5].ToString());
                }
                //处[center][/center]标记
                r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[em]
                r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[img][/img]标记
                r = new Regex(@"(\[img\])([ \S\t]*?)(\[\/img\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[file][/file]标记
                r = new Regex(@"(\[file\])([ \S\t]*?)(\[\/file\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[flash=x,y][/flash]标记
                r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[4].ToString());
                }
                //处[flash][/flash]标记
                r = new Regex(@"(\[flash\])([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[swf][/swf]标记
                r = new Regex(@"(\[swf\])([ \S\t]*?)(\[\/swf\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[dir=x][/dir]标记
                r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[4].ToString());
                }
                //处[rm=x,y][/rm]标记
                r = new Regex(@"(\[rm=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[5].ToString());
                }
                //处[mp=x,y][/mp]标记
                r = new Regex(@"(\[mp=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[5].ToString());
                }
                //处[qt=x][/qt]标记
                r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[4].ToString());
                }
                //处[QUOTE][/QUOTE]标记
                r = new Regex(@"(\[QUOTE\])((.|\n)*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[CODE][/CODE]标记
                r = new Regex(@"(\[CODE\])((.|\n)*?)(\[\/CODE\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[move][/move]标记
                r = new Regex(@"(\[move\])((.|\n)*?)(\[\/move\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[FLY][/FLY]标记
                r = new Regex(@"(\[fly\])((.|\n)*?)(\[\/fly\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[image][/image]标记
                r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[url][/url]标记
                r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                //处[url=xxx][/url]标记
                r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
                //处理[download][/download]
                r = new Regex(@"(\[download=([ \S\t]*?)\])([ \S\t]*?)(\[\/download\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), m.Groups[3].ToString());
                }
            }
            return result;
        }

        /// <summary>
        /// UBB解析处理方法
        /// </summary>
        /// <param name="content">要解析的字符串</param>
        /// <returns></returns>
        public static string UBB(string content)
        {
            string result = string.Empty;
            if (content != null && content != string.Empty)
            {
                Regex r;
                Match m;
                result = content;
                //处[b][/b]标记
                r = new Regex(@"(\[b\])((.|\n)*?)(\[\/b\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2].ToString() + "</B>");
                }
                //处[i][/i]标记
                r = new Regex(@"(\[i\])((.|\n)*?)(\[\/i\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2].ToString() + "</I>");
                }
                //处[u][/u]标记
                r = new Regex(@"(\[u\])([ \S\t]*?)(\[\/u\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2].ToString() + "</U>");
                }
                //处[sup][/sup]标记
                r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2].ToString() + "</SUP>");
                }
                //处[sub][/sub]标记
                r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2].ToString() + "</SUB>");
                }
                //处[email][/email]标记
                r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" + m.Groups[2].ToString() + "</A>");
                }
                //处[email=xxx][/email]标记
                r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\">" + m.Groups[3].ToString() + "</A>");
                }
                //处[size=x][/size]标记
                r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<FONT SIZE=" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</FONT>");
                }
                //处[color=x][/color]标记
                r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<FONT COLOR=" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</FONT>");
                }
                //处[font=x][/font]标记
                r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<FONT FACE=" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</FONT>");
                }
                //处理图片链接
                r = new Regex(@"(\[picture\])([ \S\t]*?)(\[\/picture\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<a target=_blank href='" + m.Groups[2].ToString() + "'><IMG SRC='" + m.Groups[2].ToString() + "' border=0 onload='javascript:if(this.width > 780) this.width=780'></a>");
                }
                //处理[align=x][/align]
                r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<P align=" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</P>");
                }
                //处[H=x][/H]标记
                r = new Regex(@"(\[h=([1-6])\])([ \S\t]*?)(\[\/h\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<H" + m.Groups[2].ToString() + ">" + m.Groups[3].ToString() + "</H" + m.Groups[2].ToString() + ">");
                }
                //处理[list=x][*][/list]
                r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    string strLI = m.Groups[5].ToString();
                    Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
                    Match mLI;
                    for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
                    {
                        strLI = strLI.Replace(mLI.Groups[0].ToString(), "<LI>" + mLI.Groups[1]);
                    }
                    result = result.Replace(m.Groups[0].ToString(), "<UL TYPE=\"" + m.Groups[3].ToString() + "\"><B>" + m.Groups[4].ToString() + "</B>" + strLI + "</UL>");
                }
                //处[SHADOW=x][/SHADOW]标记
                r = new Regex(@"(\[shadow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/shadow\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2].ToString() + "STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3].ToString() + ",STRENGTH=" + m.Groups[4].ToString() + ")>" + m.Groups[5].ToString() + "</TABLE>");
                }
                //处[glow=x][/glow]标记
                r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([ \S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2].ToString() + "  STYLE=FILTER:GLOW(COLOR=" + m.Groups[3].ToString() + ", STRENGTH=" + m.Groups[4].ToString() + ")>" + m.Groups[5].ToString() + "</TABLE>");
                }
                //处[center][/center]标记
                r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2].ToString() + "</CENTER>");
                }
                //处[em]
                r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<img src=emot/em" + m.Groups[2].ToString() + ".gif border=0 align=middle>");
                }
                //处[img][/img]标记
                r = new Regex(@"(\[img\])([ \S\t]*?)(\[\/img\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<a target=_blank href='" + m.Groups[2].ToString() + "'><IMG SRC='" + m.Groups[2].ToString() + "' border=0 onload='javascript:if(this.width > 780) this.width=780'></a>");
                }
                //处[file][/file]标记
                r = new Regex(@"(\[file\])([ \S\t]*?)(\[\/file\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<img src=images/download.gif>&nbsp;<a target=_blank href='" + m.Groups[2].ToString() + "'>" + m.Groups[2].ToString() + "</a>");
                }
                //处[flash=x,y][/flash]标记
                r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<a href='" + m.Groups[4].ToString() + "' TARGET=_blank><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><PARAM NAME=movie VALUE='" + m.Groups[4].ToString() + "'><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src='" + m.Groups[4].ToString() + "' quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + ">" + m.Groups[4].ToString() + "</embed></OBJECT>");
                }
                //处[flash][/flash]标记
                r = new Regex(@"(\[flash\])([ \S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<a href='" + m.Groups[2].ToString() + "' TARGET=_blank><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=400 height=400><PARAM NAME=movie VALUE='" + m.Groups[2].ToString() + "'><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src='" + m.Groups[2].ToString() + "' quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=400 height=400>" + m.Groups[2].ToString() + "</embed></OBJECT>");
                }
                //处[swf][/swf]标记
                r = new Regex(@"(\[swf\])([ \S\t]*?)(\[\/swf\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<a href='" + m.Groups[2].ToString() + "' TARGET=_blank><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=400 height=400><PARAM NAME=movie VALUE='" + m.Groups[2].ToString() + "'><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src='" + m.Groups[2].ToString() + "' quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=400 height=400>" + m.Groups[2].ToString() + "</embed></OBJECT>");
                }
                //处[dir=x][/dir]标记
                r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><param name=src value=" + m.Groups[4].ToString() + "><embed src=" + m.Groups[4].ToString() + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "></embed></object>");
                }
                //处[rm=x,y][/rm]标记
                r = new Regex(@"(\[rm=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + "><PARAM NAME=SRC VALUE='" + m.Groups[5].ToString() + "'><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=" + m.Groups[4].ToString() + "></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[2].ToString() + "><PARAM NAME=SRC VALUE='" + m.Groups[5].ToString() + "'><PARAM NAME=AUTOSTART VALUE=" + m.Groups[4].ToString() + "><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
                }
                //处[mp=x,y][/mp]标记
                r = new Regex(@"(\[mp=)(\d*?),(\d*?),([\S\t]*?)\]([ \S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + " ><PARAM NAME=AUTOSTART VALUE=" + m.Groups[4].ToString() + "><param name=Filename value='" + m.Groups[5].ToString() + "'><PARAM NAME=showstatusbar VALUE=1><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src='" + m.Groups[5].ToString() + "'  width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + " showstatusbar=1></embed></object>");
                }
                //处[qt=x][/qt]标记
                r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([ \S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<embed src=" + m.Groups[4].ToString() + " width=" + m.Groups[2].ToString() + " height=" + m.Groups[3].ToString() + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
                }
                //处[QUOTE][/QUOTE]标记
                r = new Regex(@"(\[QUOTE\])((.|\n)*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<DIV class=Quote>" + m.Groups[2].ToString() + "</DIV>");
                }
                //处[CODE][/CODE]标记
                r = new Regex(@"(\[CODE\])((.|\n)*?)(\[\/CODE\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<DIV class=Code>" + m.Groups[2].ToString() + "</DIV>");
                }
                //处[move][/move]标记
                r = new Regex(@"(\[move\])((.|\n)*?)(\[\/move\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
                }
                //处[FLY][/FLY]标记
                r = new Regex(@"(\[fly\])((.|\n)*?)(\[\/fly\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2].ToString() + "</MARQUEE>");
                }
                //处[image][/image]标记
                r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<img src=\"" + m.Groups[2].ToString() + "\" border=0 align=middle><br>");
                }
                //处[url][/url]标记
                r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" + m.Groups[2].ToString() + "</A>");
                }
                //处[url=xxx][/url]标记
                r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\">" + m.Groups[3].ToString() + "</A>");
                }
                //处理[download][/download]
                r = new Regex(@"(\[download=([ \S\t]*?)\])([ \S\t]*?)(\[\/download\])", RegexOptions.IgnoreCase);
                for (m = r.Match(result); m.Success; m = m.NextMatch())
                {
                    result = result.Replace(m.Groups[0].ToString(), "<img src=images/download.gif>&nbsp;<a target=_blank href='" + m.Groups[3].ToString() + "'>" + m.Groups[2].ToString() + "</a>");
                }
            }
            return result;
        }

        /// <summary>
        /// 添加次方值
        /// </summary>
        /// <param name="Number">要添加的值，以,号分隔的数字值</param>
        /// <returns></returns>
        public static int BuildPow(List<int> numbers)
        {
            int result = 0;
            foreach (int number in numbers)
            {
                result += BuildPow(number);
            }
            return result;
        }

        /// <summary>
        /// 添加次方值
        /// </summary>
        /// <param name="Number">要添加的值</param>
        /// <returns></returns>
        public static int BuildPow(int Number)
        {
            return (int)Math.Pow(2, Number);
        }

        /// <summary>
        /// 检查是否包含某个数的次方值
        /// </summary>
        /// <param name="totalNumber">总值</param>
        /// <param name="checkNumber">要检查的数</param>
        /// <returns></returns>
        public static bool CheckPow(int totalNumber, int checkNumber)
        {
            int tempResult = (int)Math.Pow(2, checkNumber);
            return (totalNumber & tempResult) == tempResult;
        }

        /// <summary>
        /// 将数组转成以sepearator分隔的字符串
        /// </summary>
        /// <param name="content"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string Join(List<string> content, string seperator)
        {
            if (content.Count > 0)
            {
                string result = content[0];
                for (int i = 1; i < content.Count; i++)
                {
                    result += seperator + content[i];
                }
                return result;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 将数组转成以","分隔的字符串
        /// </summary>
        /// <param name="content"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string Join(IEnumerable<string> content)
        {
            string result = string.Empty;
            using (IEnumerator<string> enumerator = content.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    result += (result == string.Empty) ? enumerator.Current : "," + enumerator.Current;
                }
            }
            return result;
        }
        /// <summary>
        /// base64编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToBase64(this string str)
        {
            return Convert.ToBase64String(Encoding.Default.GetBytes(str));
        }
        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToUrlCode(this string str)
        {
            return HttpContext.Current.Server.UrlEncode(str);
        }
    }
}
